﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sigavat.Entities;

namespace Sigavat.Repository
{
    public class PromotorRepository :MasterRepository, IPromotorRepository
    {
        public List<Promotor> GetAllFromPromotores()
        {
            var query = (from p in Context.Promotores
                         .Include("CentroComercial")
                select p).Take(15);
            return query.ToList();
        }

        public List<Promotor> GetFromPromotoresByCriterio(string criterio)
        {
            var query = from p in Context.Promotores
                            .Include("CentroComercial")
                        select p;
            if (criterio!=null && criterio!=string.Empty)
            {
                query = (from p in query
                    where p.Nombres.ToUpper().Contains(criterio.ToUpper()) ||
                          p.ApPaterno.ToUpper().Contains(criterio.ToUpper()) ||
                          p.ApMaterno.ToUpper().Contains(criterio.ToUpper()) ||
                          p.CentroComercial.NombreCentroC.ToUpper().Contains(criterio.ToUpper())
                          
                    select p).Take(15);

            }
            return query.ToList();
        }

        public Promotor GetFromPromotorBy(int id)
        {
            var query = from p in Context.Promotores
                        .Include("CentroComercial")
                select p;
            if (id!=null)
            {
                query = from p in query
                    where p.Id.Equals(id)
                    select p;
            }
            return query.SingleOrDefault();
        }

        public void AddPromotor(Promotor promotor)
        {
            Context.Promotores.Add(promotor);
            Context.SaveChanges();
        }

        public void UpdatePromotor(Promotor promotor)
        {
            Context.Entry(promotor).State = EntityState.Modified;
            Context.SaveChanges();
        }

        public void RemovePromotor(int id)
        {
            var existe = Context.Promotores.Find(id);

            if (existe!=null)
            {
                Context.Promotores.Remove(existe);
                Context.SaveChanges();
            }
        }
    }
}
